//
//  JddChangePwdViewController.m
//  PlayBall
//
//  Created by superman on 14-8-22.
//
//

#import "JddChangePwdViewController.h"
#import "JddViewControllerManager.h"
#import "JddParameterBuilder.h"

@interface JddChangePwdViewController ()

@end

@implementation JddChangePwdViewController
@synthesize xinPwdTextField;
@synthesize conformNewPwdTextField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"XiuGaiMiMa"];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"XiuGaiMiMa"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"修改密码"];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
//    //self.navigationItem.hidesBackButton = YES;
    
//    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    backBtn.frame = CGRectMake(0, 0, 12, 19);
//    backBtn.backgroundColor = [UIColor clearColor];
//    [backBtn setImage:[UIImage imageNamed:@"navbar_back"] forState:UIControlStateNormal];
//    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
//    self.navigationItem.leftBarButtonItem = backItem;
    
    self.xinPwdTextField.delegate = self;
    self.xinPwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.xinPwdTextField.secureTextEntry = YES;
    self.conformNewPwdTextField.delegate = self;
    self.conformNewPwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.conformNewPwdTextField.secureTextEntry = YES;
    
    //添加手势，点击任意处隐藏键盘
    UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    oneTap.delegate = self;
    oneTap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:oneTap];
}

- (void)back
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"你不打算修改新密码了吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    //[self.navigationController popViewControllerAnimated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView.message isEqualToString:@"你不打算修改新密码了吗？"]) {
        if (buttonIndex == 0) {
            
        }else{
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
        }
    }else if([alertView.message isEqualToString:@"密码修改成功，请重新登录玩球球"])
    {
        if (buttonIndex == 0) {
            
           // NSLog(@"============%d",self.navigationController.viewControllers.count);
            if (self.navigationController.viewControllers.count == 6) {
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
            }else if(self.navigationController.viewControllers.count == 8){
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:4] animated:YES];
            }else if(self.navigationController.viewControllers.count == 9)
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:5] animated:YES];
        }else{
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
        }
    }
}
//自动登入的回调函数
//- (void)LogonOperationResult:(NSData *)result{
//    
//}
//收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.xinPwdTextField resignFirstResponder];
    [self.conformNewPwdTextField resignFirstResponder];
    return YES;
}
- (void)hideKeyBoard
{
    [self.xinPwdTextField resignFirstResponder];
    [self.conformNewPwdTextField resignFirstResponder];
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)simmitBtnClicked:(id)sender {
    
    if (![xinPwdTextField.text length] || ![conformNewPwdTextField.text length]){
        [JddCommonFunction messageBoxWithMessage:@"新密码或确认密码不能为空"];
    }
    else if ([xinPwdTextField.text length] <6 ||[conformNewPwdTextField.text length]<6) {
        [JddCommonFunction messageBoxWithMessage:@"密码不能少于六位"];
    }
    else if (![xinPwdTextField.text isEqualToString:conformNewPwdTextField.text]) {
        [JddCommonFunction messageBoxWithMessage:@"输入的密码不一致"];
    }
    else{
        JddXiugaiPwd *operation = [[JddXiugaiPwd alloc] init];
        operation.delegate = self;
        operation.userName = self.userName;
        operation.phoneNum = self.phoneNum;
        operation.xinPwd = self.conformNewPwdTextField.text;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
}
//提交新密码的回调函数
- (void)XiugaiPwdResult:(NSData *)result
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
    if (result) {
        NSString *uId = [JddDataParser parseLoginResult:result];
        if (uId.length == 0) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
        }
       
        if ([[dic objectForKey:@"Code"]intValue] == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"密码修改成功，请重新登录玩球球" delegate:self cancelButtonTitle:nil otherButtonTitles:@"马上登录", nil];
            [alert show];
            
        }
    }
}

@end
